package com.buka.goods.config;

import com.google.common.collect.Maps;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class RabbitMqConfig {
    //队列
    public static final String CART_QUEUE = "cart.queue";
    public static final String GOODS_QUEUE = "goods.queue";
    public static final String TASK_QUEUE = "task.queue";
    public static final String ORDER_QUEUE = "order.queue";
    public static final String ALIPAY_QUEUE = "alipay.queue";
    public static final String ADD_INVENTORY_QUEUE = "addInventory.queue";
    public static final String PAY_QUEUE = "pay.queue";
    public static final String NOTIFY_QUEUE = "notify.queue";
    public static final String TIME_OUT_QUEUE = "time.out.queue";

    //交换器
    public static final String PAY_EXCHANGE = "pay.exchange";

    @Bean(CART_QUEUE)
    public Queue AD_QUEUE() {
        return new Queue(CART_QUEUE);
    }
    @Bean(GOODS_QUEUE)
    public Queue GOODS_QUEUE() {
        return new Queue(GOODS_QUEUE);
    }
    @Bean(TASK_QUEUE)
    public Queue TASK_QUEUE() {
        return new Queue(TASK_QUEUE);
    }
    @Bean(ORDER_QUEUE)
    public Queue ORDER_QUEUE() {
        return new Queue(ORDER_QUEUE);
    }
    @Bean(ALIPAY_QUEUE)
    public Queue ALIPAY_QUEUE() {
        return new Queue(ALIPAY_QUEUE);
    }
    @Bean(TIME_OUT_QUEUE)
    public Queue TIME_OUT_QUEUE() {
        return new Queue(TIME_OUT_QUEUE);
    }
    @Bean(ADD_INVENTORY_QUEUE)
    public Queue ADD_INVENTORY_QUEUE() {
        return new Queue(ADD_INVENTORY_QUEUE);
    }
    @Bean(NOTIFY_QUEUE)
    public Queue NOTIFY_QUEUE() {
        return new Queue(NOTIFY_QUEUE);
    }

    @Bean(PAY_QUEUE)
    public Queue PAY_QUEUE() {
        Map<String, Object> args = Maps.newHashMap();
        args.put("x-dead-letter-exchange",PAY_EXCHANGE);
        args.put("x-message-ttl",1800000);
        return new Queue(PAY_QUEUE,
                true,false,false,args);
    }

    //声明交换机
    @Bean(PAY_EXCHANGE)
    public Exchange PAY_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(PAY_EXCHANGE).durable(true).build();
    }

    //队列绑定交换机，指定routingKey
    @Bean
    public Binding DEL_GOODS_EXCHANGE_QUEUE(@Qualifier(TIME_OUT_QUEUE) Queue queue, @Qualifier(PAY_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(TIME_OUT_QUEUE).noargs();
    }
}
